package com.cjm.thread.zhonjianjian.cthreadlocal;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class DemoOne {

    public static void main(String[] args) {
        ThreadLocal<String> threadLocal = new ThreadLocal();
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for (int j = 0; j < 3; j++) {
            executorService.submit(() -> {
                int i = 0;
                String name = Thread.currentThread().getName();
                while (i < 5) {
                    String s = threadLocal.get();
                    if (s == null) {
                        threadLocal.set(name);
                    }

                    System.err.println("thread name = " + name + ";thread local = " + threadLocal.get());
                    i++;
                }
            });
        }

        for (int j = 0; j < 3; j++) {
            executorService.submit(() -> {
                int i = 0;
                String name = Thread.currentThread().getName();
                while (i < 5) {
                    if ("pool-1-thread-3".equals(name)) {
                        threadLocal.set(name + ";第二次啦");
                    }
                    System.err.println("thread name = " + name + ";thread local = " + threadLocal.get());
                    i++;
                }
            });
        }

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.err.println("over");
        executorService.shutdown();

//        try {
//            Thread.currentThread().join();
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
    }
}
